<template>
  <!-- 添加工作集 -->
  <a-modal
    v-model="show"
    destroyOnClose
    title="添加工作集"
    :maskClosable="false"
    @cancel="workSetCancel"
  >
    <a-input-search
      v-model="searchText"
      placeholder="输入关键词搜索"
      style="margin-bottom: 8px"
      @change="handleSearch"
    />
    <a-tree
      v-model="checkedKeys"
      checkable
      :tree-data="filteredWorkSetData"
      :replaceFields="{
        children: 'JobGroupList',
        title: 'JobGroupName',
        key: 'JobGroupID',
      }"
    />
    <template slot="footer">
      <a-button key="back" @click="workSetCancel">取消</a-button>
      <a-button
        :loading="loading"
        key="submit"
        type="primary"
        @click="workSetOk"
        >提交</a-button
      >
    </template>
  </a-modal>
</template>

<script>
import { getJobGroupToList } from "@/api/JobRouteOperationRelation";
import {
  insJobOperationRelations,
  getStdOperationListToStdOperationID,
} from "@/api/StdOperation";
export default {
  props: ["show", "item"],
  data() {
    return {
      workSetData: [], //所有工作集
      filteredWorkSetData: [], //过滤后的工作集
      checkedKeys: [], //已关联项
      upLoad: false, //是否全局更新
      loading: false,
      searchText: "", //搜索关键词
    };
  },
  methods: {
    /* 处理搜索 */
    handleSearch() {
      if (!this.searchText) {
        this.filteredWorkSetData = this.workSetData;
        return;
      }

      const filterNode = (node) => {
        if (
          node.JobGroupName.toLowerCase().includes(
            this.searchText.toLowerCase()
          )
        ) {
          return true;
        }

        if (node.JobGroupList && node.JobGroupList.length > 0) {
          const filteredChildren = node.JobGroupList.filter((child) =>
            child.JobGroupName.toLowerCase().includes(
              this.searchText.toLowerCase()
            )
          );
          return filteredChildren.length > 0;
        }

        return false;
      };

      this.filteredWorkSetData = this.workSetData.filter(filterNode);
    },

    /* 关闭工作集关联弹窗 */
    workSetCancel() {
      this.checkedKeys = []; //清除选中项
      this.$emit("update:show", false);
    },
    //提交关联工作集
    async sumbitWork() {
      let list = [];
      this.loading = true;
      try {
        let { StdOperationID } = this.item;
        this.checkedKeys.map((item) => {
          let obj = {
            StdOperationID: StdOperationID,
            jobGroupID: item,
          };
          list.push(obj);
        });
        let res = await insJobOperationRelations(this.upLoad, list);
        this.$message.success("关联成功");
        this.workSetCancel();
        this.$emit("updata");
      } catch (error) {
      } finally {
        this.loading = false;
      }
    },
    /* 提交工作集关联弹窗 */
    workSetOk() {
      let _this = this;
      this.$confirm({
        title: "提示",
        content: `是否全局更新该工序关联的工作集`,
        okText: "是",
        cancelText: "否",
        onOk() {
          _this.upLoad = true;
          _this.sumbitWork();
        },
        onCancel() {
          _this.upLoad = false;
          _this.sumbitWork();
        },
      });
    },

    //查询工作集
    async getJobGroupToList() {
      let { RouteOperationID } = this.item;
      let data = {
        RouteOperationID,
      };
      let res = await getJobGroupToList(data);
      this.workSetData = res.Data;
      this.filteredWorkSetData = res.Data;
      this.workSetData.map((item) => {
        item.JobGroupName = item.JobGroupTypeName;
        item.JobGroupID = item.JobGroupTypeID;
        item.disabled = true;
        item.JobGroupList.map((itm, index) => {
          itm.JobGroupName = itm.JobGroupName + " - " + itm.JobTempletVisionOut;
        });
      });
    },
    //查询已绑定的工作集
    async getStdOperationListToStdOperationID() {
      let { StdOperationID } = this.item;
      let data = {
        StdOperationID,
      };
      let res = await getStdOperationListToStdOperationID(data);

      let list = [];
      res.Data.map((item, index) => {
        list.push(item.JobGroupID);
      });
      this.checkedKeys = list;
    },
  },
  watch: {
    show: {
      handler(newValue, oldValue) {
        if (newValue) {
          this.getJobGroupToList();
          this.getStdOperationListToStdOperationID();
        }
      },
    },
  },
};
</script>

<style></style>